(function (mui, $) {
var page = window.page = {}, cache = [];
	
var _Data = {
	'token'       : app.store('token'),
	'page'        : 0,
	'nums'        : 20,
};

function next (cb) {
	app.log('请求参数：', mui.extend({}, _Data, {'page':_Data.page + 1}));
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Member/Index/withdrawalsList'),
		'data'     : mui.extend({}, _Data, {'page':_Data.page + 1}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('取得提现记录失败：', res);
		app.toast('取得提现记录失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('提现记录', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
	})
	;
}

function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(pullrefresh.element).find('.mui-scroll>.scroll-data').empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh();
	});
}
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		var ele = app.tpl($(dom).attr('data-tpl'), res);
		if ($(ele).hasClass('no-data')) {
			if (0 == _Data.page) { $(dom).empty(); } else { $(ele).remove(); ele = null; }
		}
		$(dom).append(ele);
		pullrefresh.endPullupToRefresh();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

mui.plusReady(function() {
	setTimeout(call_on_pull_down, mui.os.ios ? 300 : 1000);
});

// 普通页面跳转
$('body').delegate('[data-url]', 'tap', function () {
	app.open($(this).attr('data-url'));
});

})(mui, $);